package net.zoneland.knowledge.controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Optional;
import net.zoneland.knowledge.model.PartyCalendarToday;
import net.zoneland.knowledge.model.ResponseResult;
import net.zoneland.knowledge.service.PartyCalendarTodayService;

/**
 * 党史今日查询.
 * @author bcc
 */
@RestController()
@RequestMapping("/partycalendartoday")
public class PartyCalendarTodayController {

    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(PartyCalendarTodayController.class);

    @Autowired
    private PartyCalendarTodayService partyCalendarTodayService;

    /**
     * 显示党史今日图片.
     * @param monthDate 日期 MM-dd
     * @param response http响应
     * @throws IOException 异常
     */
    @GetMapping(value = "/showInfo")
    public void showInfo(final String monthDate, final ServletResponse response) throws IOException {

        final ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType(ContentType.IMAGE_JPEG.getMimeType());
        Optional.ofNullable(monthDate)
                .map(partyCalendarTodayService::queryByDateString)
                .ifPresent(partyCalendarToday -> writePictureInfoToOutputSteam(partyCalendarToday, outputStream));
    }

    /**
     * 显示党史今日文章内容.
     * @param monthDate 日期 MM-dd
     * @return 响应
     */
    @ResponseBody
    @GetMapping(value = "/getPartyArticleContent")
    public ResponseResult<String> getPartyArticleContent(final String monthDate) {

        return Optional.ofNullable(monthDate)
                .map(partyCalendarTodayService::queryByDateString)
                .map(PartyCalendarToday::getContent)
                .map(ResponseResult::new)
                .orElseGet(ResponseResult::new);
    }


    /**
     * 将图片信息写入output流.
     * @param partyCalendarToday 党史今日图片信息
     * @param outputStream 输出流
     */
    private static void writePictureInfoToOutputSteam(@NonNull final PartyCalendarToday partyCalendarToday,
                                               final ServletOutputStream outputStream) {
        try {
            // 后台存的图片是base64格式的，接口希望能返回图片的数据流，需要转码
            final byte[] picData = Base64.getDecoder().decode(partyCalendarToday.getPictureFile());

            IOUtils.copy(new ByteArrayInputStream(picData), outputStream);
        } catch (IOException e) {
            LOGGER.error("writePictureInfoToOutputSteam error monthDate:{}", partyCalendarToday.getPartyDate(), e);
        }
    }

}
